<?php

Raise::load('core.RaiseHashDigest');

/**
 * TestCoreRaiseHashDigest class
 * Testing unit of 'phpraise/core/RaiseHashDigest.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseHashDigest extends RaiseTest {
    
    public static function testClass(){
        self::assertClassExists('RaiseHashDigest');
        self::assertClassExtends('RaiseHashDigest', 'RaiseStaticClass');
        self::assertClassMethod('RaiseHashDigest', 'crc32');
        self::assertClassMethod('RaiseHashDigest', 'md5');
        self::assertClassMethod('RaiseHashDigest', 'sha1');
        self::assertClassMethod('RaiseHashDigest', 'sha256');
        self::assertClassMethod('RaiseHashDigest', 'generateSalt');
        self::assertClassMethod('RaiseHashDigest', 'generatePasswordHash');
        self::assertClassMethod('RaiseHashDigest', 'extractPasswordSalt');
        self::assertClassMethod('RaiseHashDigest', 'hash');
        self::assertClassMethod('RaiseHashDigest', 'algorithms');
    }

    public static function testCrc32(){
        $s = 'testing string';
        self::assertStrictEqual(RaiseHashDigest::crc32($s), 'af37a819');
        self::assertStrictEqual(strlen(RaiseHashDigest::crc32($s)), RaiseHashDigest::CRC32_LENGTH);
    }

    public static function testMd5(){
        $s = 'testing string';
        self::assertStrictEqual(RaiseHashDigest::md5($s), '26dec0b019e06aaf67771410aac1e4d3');
        self::assertStrictEqual(strlen(RaiseHashDigest::md5($s)), RaiseHashDigest::MD5_LENGTH);
    }

    public static function testSha1(){
        $s = 'testing string';
        self::assertStrictEqual(RaiseHashDigest::sha1($s), '8ddc1738150524a38bbb2c3e667c2c14f091534d');
        self::assertStrictEqual(strlen(RaiseHashDigest::sha1($s)), RaiseHashDigest::SHA1_LENGTH);
    }

    public static function testSha256(){
        $s = 'testing string';
        self::assertStrictEqual(RaiseHashDigest::sha256($s), '5c7f513f870487396066e7792ae7338aab6039d13fa142ea8cf8df9d87f6da70');
        self::assertStrictEqual(strlen(RaiseHashDigest::sha256($s)), RaiseHashDigest::SHA256_LENGTH);
    }

    public static function testGenerateSalt(){
        $p = 'testPwd';
        self::assertStrictEqual(strlen(RaiseHashDigest::generateSalt($p)), RaiseHashDigest::SHA1_LENGTH);
        self::assertType(RaiseHashDigest::generateSalt($p), 'string');
    }

    public static function testGeneratePwdHash(){
        $p = 'testPwd';
        self::assertStrictEqual(strlen(RaiseHashDigest::generatePasswordHash($p)), RaiseHashDigest::SHA1_LENGTH + RaiseHashDigest::SHA256_LENGTH);
        self::assertType(RaiseHashDigest::generatePasswordHash($p), 'string');
    }

    public static function testExtractSalt(){
        $p = 'testPwd';
        $s = RaiseHashDigest::generateSalt($p);
        $h = RaiseHashDigest::generatePasswordHash($p, $s);
        self::assertStrictEqual(RaiseHashDigest::extractPasswordSalt($h, $p), $s);
        self::assertType(RaiseHashDigest::extractPasswordSalt($h, $p), 'string');
        self::assertStrictEqual(strlen(RaiseHashDigest::extractPasswordSalt($h, $p)), RaiseHashDigest::SHA1_LENGTH);
    }

}
